package com.gemme.gemme.view;

import android.content.Context;
import android.graphics.Rect;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gemme.gemme.R;
import com.gemme.gemme.manager.DataManager;

import java.util.HashMap;

public class ChatInputDialog {
    BottomDialog dialog;
    private Context mContext;
    private OnStrChangeLIstener onStrChangeLIstener;

    public void showDialog(Context mContext){
//new int[]{R.id.close}
        dialog = new BottomDialog(mContext, R.layout.chat_input_dialog,
                null);
        this.mContext = mContext;

        dialog.show();
        initView();
        dialog.setOnBottomItemClickListener(new BottomDialog.OnBottomItemClickListener() {
            @Override
            public void onBottomItemClick(BottomDialog dialog, View view) {
                //
            }
        });

    }


    EditText et_search;
    private void initView() {
        View view = dialog.getView();
        /*Window win = dialog.getWindow();
        WindowManager.LayoutParams params = win.getAttributes();
        win.setSoftInputMode(params.SOFT_INPUT_ADJUST_NOTHING);*/

        et_search = view.findViewById(R.id.et_search);
        show(et_search);
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    System.out.println("");
                    String commentStr = et_search.getText() + "";
                    if(onStrChangeLIstener != null)
                        onStrChangeLIstener.onChangeLIstener(commentStr);

                    et_search.setText("");
                }
                return false;
            }
        });
    }

    public  void show( EditText content){
        content.setFocusable(true);
        content.setFocusableInTouchMode(true);
        content.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) content.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    content.requestFocus();
                    imm.showSoftInput(content, 0);
                }
            }
        },200);
    }

    public void dismiss(){
        if(dialog != null)
            dialog.dismiss();
    }

    public void setOnStrChangeLIstener(OnStrChangeLIstener onStrChangeLIstener){

        this.onStrChangeLIstener = onStrChangeLIstener;
    }


}
